<?php

namespace Api\Controller;

use Think\Controller;

class ApiController extends Controller
{
    public function login($username, $password)
    {
        $where ["user_name"] = $username;
        $where ["password"] = md5($password);

        $result = M("Admin")->where($where)->find();

        if ($result) {
            return $result;
        }
    }

    public function getsetting()
    {
        $result = M("Info")->find();
        if ($result) {
            return $result;
        }
    }

    public function setting($name, $notification)
    {
        $data ["id"] = 1;
        $data ["name"] = $name;
        $data ["notification"] = $notification;
        $result = M("Info")->save($data);
        if ($result) {
            return $result;
        }
    }

    public function getalipay()
    {
        $result = M("Alipay")->find();
        if ($result) {
            return $result;
        }
    }

    public function setalipay($alipayname, $partner, $key)
    {
        $select = M("Alipay")->select();
        if ($select) {
            $data ["id"] = 1;
            $data ["alipayname"] = $alipayname;
            $data ["partner"] = $partner;
            $data ["key"] = $key;

            $result = M("Alipay")->save($data);
        } else {
            $data ["alipayname"] = $alipayname;
            $data ["partner"] = $partner;
            $data ["key"] = $key;

            $result = M("Alipay")->add($data);
        }

        if ($result) {
            return $result;
        }
    }

    public function getarraymenu()
    {
        $result = M("Menu")->select();

        Vendor("tree.Tree");
        $tree = new \Tree (); // new 之前请记得包含tree文件!
        $tree->tree($result); // 数据格式请参考 tree方法上面的注释!

        // 如果使用数组, 请使用 getArray方法
        $result = $tree->getArray();

        // 下拉菜单选项使用 get_tree方法
        // $tree->get_tree();
        if ($result) {
            return $result;
        }
    }

    public function getmenu()
    {
        $result = M("Menu")->select();
        if ($result) {
            return $result;
        }
    }

    public function addmenu($parent, $name, $addmenu, $icon)
    {
        if ($addmenu == 0) {
            $data ["name"] = $name;
            $data ["pid"] = $parent;
            /*$data ["pid"] = 0;*/
            $data['icon'] = $icon;
            $result = M("Menu")->add($data);
            if ($result) {
                return $result;
            }
        } else {
            $data ["id"] = $addmenu;
            $data ["name"] = str_replace("│ ", "", $name);
            $data ["pid"] = $parent;
            if ($icon !== '') {
                $data['icon'] = $icon;
            }
            $result = M("Menu")->save($data);
            if ($result) {
                return $result;
            }
        }
    }

    public function delmenu($id)
    {
        $result = M("Menu")->where(array(
            'id' => $id
        ))->delete();

        if ($result) {
            return $result;
        }
    }

    public function getmenuvalue($menu_id)
    {
        $result = M("Menu")->where(array(
            "id" => $menu_id
        ))->find();
        if ($result) {
            return $result ["name"];
        }
    }

    public function getgood()
    {
        $result = M("Good")->select();
        if ($result) {
            return $result;
        }
    }

    public function addgood($data)
    {
        //添加图片是新增的
        $pics = array();
        if ($data["id"]) {
            $result = M("Good")->save($data);
            foreach ($data['pic'] as $key1 => $val1) {
                array_push($pics, array('good_id' => $data['id'], 'pic' => $val1));
            }
        } else {
            $result = M("Good")->add($data);
            foreach ($data['pic'] as $key1 => $val1) {
                array_push($pics, array('good_id' => $result, 'pic' => $val1));
            }
        }
        M('GoodPic')->addAll($pics);
        if ($result) {
            return $result;
        }
    }

    public function delgood($id)
    {
        $result = M("Good")->where(array(
            "id" => $id
        ))->delete();
        if ($result) {
            return $result;
        }
    }

    /**
     * 删除用户
     * @param $id
     * @return mixed
     */
    public function delUser($id)
    {
        //首先判断用户存在
        $result = M("User")->where("id='$id'")->find();
        if($result){
            $data['flag'] = "2";
            M('User')->where(array(
                "id" => $id
            ))->save($data);
        }else{
            return 2;
        }
        /*$result = M("User")->where(array(
            "id" => $id
        ))->save();
        if ($result) {
            return $result;
        }*/
    }

    /**
     * 修改用户信息
     * @param $id
     * @return mixed
     */
    public function editUser($id)
    {
        //根据id查找用户的信息
        $result = M("User")->where(array(
            "id" => $id
        ))->find();

        if($result){
            return $result;
        }
    }

    /**
     * 保存用户信息
     * @param $data
     */
    public function saveUser($data)
    {
        if($data && is_array($data)){
            $id = $data['id'];
            //查找是否存在
            $user = M("User")->where(array(
                "id" => $id
            ))->find();
            if($user){
                $result = M("User")->where("id='$id'")->save($data);
            }else{
                $this->error("用户不存在");
            }
        }else{
            $this->error("操作失败");
        }
    }

    public function delorder($id)
    {
        $reuslt = M("Order")->where(array(
            "id" => $id
        ))->delete();
        if ($reuslt) {
            return $reuslt;
        }
    }

    public function payComplete($id)
    {
        $data ["id"] = $id;
        $data ["pay_status"] = 1;
        $result = M("Order")->save($data);
        if ($result) {
            return $result;
        }
    }

    public function getuser($uid)
    {
        $m = M("User");
        $where["uid"] = $uid;
        $result = $m->where($where)->find();
        if ($result) {
            return $result;
        }
    }
}